import java.util.*;

public class Solution740 {
    public int deleteAndEarn(int[] nums) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int num : nums) {
            map.put(num,map.getOrDefault(num,0)+1);
        }
        List<Map.Entry<Integer,Integer>> l=new ArrayList<>(map.entrySet());
        Collections.sort(l, Comparator.comparingInt(Map.Entry::getKey));
        int[] f=new int[l.size()+1];
        f[1]=l.get(0).getKey()*l.get(0).getValue();
        for (int i=2;i<=l.size();i++){
            if (l.get(i-1).getKey()-l.get(i-2).getKey()==1){
                f[i]=Math.max(f[i-2]+l.get(i-1).getKey()*l.get(i-1).getValue(),f[i-1]);
            }else {
                f[i]=f[i-1]+l.get(i-1).getKey()*l.get(i-1).getValue();
            }
        }
        return f[l.size()];
    }

    public static void main(String[] args) {
        System.out.println(new Solution740().deleteAndEarn(new int[]{2,2,3,3,3,4}));
    }
}
